pp108 : Defining XPath Expressions

Defining XPath Expressions

This topic describes the concept of defining XPath expressions.


To control the expression evaluation, you can pass dynamic information such as, variable binding, namespace binding, and so on to the XPath execution context with the help of XPathMetaInfo object. This XPathMetaInfo object can be shared across different XPath objects.
The different options for using XPathMetaInfo Object are:

  • Variable binding
  • Namespace binding
  • Function registration

Variable Binding


XPath expressions can contain variable reference that requires a value to be assigned to it. For example, the expression //personalInfo[@name=$var], will select the personalInfo elements having the value of the name attribute equal to the value of the variable reference var. To get the expression evaluated, the variable var has to be assigned a value. Here is the algorithm for doing the same:

XPath xpathObject = new XPath("/personalInfo[@name = $var]"); XPathMetaInfo xpathMetaInfoObj = new XPathMetaInfo(); XPathResult XpathResultObj = new XPathResult("Toma"); XPathMetaInfoObj.addVarBinding("var" , XpathResultObj); XPathResult res = XpathObject.evaluate(iNomNode, XpathMetaInfoObj);


Note: In the above example, iNomNode refers to the NOM handle containing the XML.

Namespace Binding


Similar to variable binding, you can bind a URI to a prefix used in an XPath expression:

XPath xpathObject = new XPath("/ns1:onalInfo[@name = $var]"); XPathMetaInfo xpathMetaInfoObj = new XPathMetaInfo(); XPathMetaInfoObj.addNamespaceBinding("ns1", "http://hello.com"); XPathResult res = XpathObject.evaluate(iNomNode, XpathMetaInfoObj);

When you use namespace specific search, ensure the following:

  • Use a prefix inside the XPath expressions to find a specific element that is originating from a particular namespace. The prefix could either be the actual prefix used in the input document or the default namespace (no prefix). But the prefix you use inside the expression should be mapped to the actual URI that you expect inside the input document.
  • For performance reasons, the namespace processing is disabled by default. However, if you use a prefix inside XPath expression, it will enable the namespace processing automatically. The expression will match nodes if the local part of the element name is matched. For example, consider following expression,
    "/mycat/name"
    and the input is:
    <cat xmlns="http://cat.com">
        <mycat>
            <name>Toma</name>
        </mycat>
    </cat>
    
    The expression will select the 'name' element even though it is originating from http://cat.com.However, for the following structure (which is semantically equivalent to the above input), the same expression will not select the nodes.
    <ns:cat xmlns:ns="http://cat.com">
        <ns:mycat>
            <ns:name>Toma</ns:name>
        </ns:mycat>
    </ns:cat>
    
    For this input you need to add a namespace binding. The above expression can be changed to
    "/ns1:mycat/ns1:name"
    Binding 'ns1' to 'http://cat.com' will do the trick.This limitation is to improve the performance and it was observed that most of the searches happen irrespective of the namespace declaration.

Function Registration


Most of the functions described by XPath1.0 and XSLT1.0 are available in Process Platform. However, for a list of missing functions, refer to Features that are not supported in Process Platform.

However, if you want to use custom functions, you must register them (for native users) from the native layer. These functions must be called from XPath expressions, using MetaInfo object. Users can associate a name with a function pointer of the type,

void (*lib_fun) ( void *pXpathContext , CXPathResult *pArg[], CXPathResult *retValue);


Note: The retValue is used to pass the result back to the engine. Initially, it will contain the number of arguments actually passed through the expression. For using custom Java functions, though a separate registration is not needed from the Java layer, the following conditions must be satisfied :

  • Java function must be static
  • The class must be set in the classpath
  • The Java method call can return Nodeset however, the Nodeset must include only those nodes which are part of the input NOM tree originally provided

    According to the XPath1.0 specification, the XPath function id(Object) selects nodes by their unique identity (ID).

    NOM does not have ID support, but XPath specification has ID aspect. As a limited work around, an user can have a particular attribute designated as the ID attribute during XPath evaluation by using the following API of theXPathMetaInfoclass:
    registerAttribute(java.lang.StringsAttr)

    This makes the XPath processor consider the registered attribute as the ID attribute and select nodes accordingly.

Related reference

XPath Parser, XPath Object and XPath Output
Understanding XPath Expressions
Optimizing XPath Expression Usage
Using XPath API

Related information

Overview of XPath